# -*- coding:utf-8 -*-
'''
输入一个复杂链表（每个节点中有节点值，以及两个指针，
一个指向下一个节点，另一个特殊指针指向任意一个节点），返回结果为复制后复杂链表的head。
（注意，输出结果中请不要返回参数中的节点引用，否则判题程序会直接返回空）

递归复制法
'''
class RandomListNode:
    def __init__(self, x):
        self.label = x
        self.next = None
        self.random = None


class Solution:
    # 返回 RandomListNode
    def Clone(self, pHead):
        # write code here
        if not pHead: return
        nextnode = self.Clone(pHead.next)
        newHead = RandomListNode(pHead.label)
        newHead.random = pHead.random
        newHead.next = nextnode
        return newHead

    def Clone_opt(self, pHead):
        if not pHead: return None
        cur = pHead
        while cur:
            tmp = RandomListNode(cur.label)
            tmp.next = cur.next
            cur.next = tmp
            cur = tmp.next
        cur  = pHead
        # find the random
        while cur:
            tmp = cur.next
            if cur.random:tmp.random = cur.random.next
            cur = tmp.next
        # to two listNode
        cur = pHead
        res = pHead.next
        while cur.next:
            tmp = cur.next
            cur.next = tmp.next
            cur = tmp
        return res
